package com.bbzn.device.client.service.impl;

import com.bbzn.device.client.constant.AuditConstant;
import com.bbzn.device.client.constant.RedisConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.SystemParamByCacheService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.bbzn.device.client.dataobject.FlowNode;
import com.bbzn.device.client.dao.process.FlowNodeMapper;
import com.bbzn.device.client.service.FlowNodeService;

@Service
public class FlowNodeServiceImpl implements FlowNodeService {

    @Resource
    private FlowNodeMapper flowNodeMapper;

    @Resource
    private SystemParamByCacheService systemParamByCacheService;

    @Override
    public int deleteByPrimaryKey(Long flowNodeId) {
        return flowNodeMapper.deleteByPrimaryKey(flowNodeId);
    }

    @Override
    public int insert(FlowNode record) {
        return flowNodeMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(FlowNode record) {
        return flowNodeMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(FlowNode record) {
        return flowNodeMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(FlowNode record) {
        return flowNodeMapper.insertSelective(record);
    }

    @Override
    public FlowNode selectByPrimaryKey(Long flowNodeId) {
        return flowNodeMapper.selectByPrimaryKey(flowNodeId);
    }

    @Override
    public int updateByPrimaryKeySelective(FlowNode record) {
        return flowNodeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FlowNode record) {
        return flowNodeMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<FlowNode> findByAll(FlowNode flowNode) {
        return flowNodeMapper.findByAll(flowNode);
    }

    @Override
    public int updateBatch(List<FlowNode> list) {
        return flowNodeMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FlowNode> list) {
        return flowNodeMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FlowNode> list) {
        return flowNodeMapper.batchInsert(list);
    }

    @Override
    public void addFlopwNode(Long flowId,List<FlowNode> record, String language) throws Exception {
        SystemParam systemParam = null;
        if (CollectionUtils.isEmpty(record)) {
            systemParam = systemParamByCacheService.getValues(RedisConstant.REDIS_ENUM, RedisConstant.TOKEN_CACHE_EXCEPTION_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        List<FlowNode> flowNodes = new ArrayList<>();
        for (FlowNode flowNode : record) {
            if (null==flowNode.getFlowNodeUserId()) {
                systemParam = systemParamByCacheService.getValues(AuditConstant.AUDIT_ENUM, AuditConstant.AUDIT_NO_FLEW_NODE_USER_ID_PREFIX + language);
                throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
            }
            if (null == flowNode.getFlowNodeLevel()) {
                systemParam = systemParamByCacheService.getValues(AuditConstant.AUDIT_ENUM, AuditConstant.AUDIT_NO_FLEW_NODE_LEVEL_PREFIX + language);
                throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
            }
            FlowNode query = new FlowNode();
            query.setFlowNodeId(flowNode.getFlowNodeId());
            flowNode.setFlowId(flowId);
            int count = flowNodeMapper.findCountByAll(query);
            if (count <= 0) {
                flowNodes.add(flowNode);
            }else{
                flowNodeMapper.updateByPrimaryKeySelective(flowNode);
            }
        }
        if (CollectionUtils.isNotEmpty(flowNodes)) {
            flowNodeMapper.batchInsert(flowNodes);
        }
    }

    @Override
    public int deleteByFlowId(Long flowId) {
        return flowNodeMapper.deleteByFlowId(flowId);
    }


}







